package com.aaa.inbound.service.impl;

import com.aaa.entity.inbound.Purchase;
import com.aaa.entity.inbound.PurchaseDetail;
import com.aaa.entity.inbound.Receive;
import com.aaa.entity.inbound.ReceiveDetail;
import com.aaa.entity.inside.Allocation;
import com.aaa.entity.inside.AllocationDetail;
import com.aaa.inbound.mapper.ReceiveMapper;
import com.aaa.inbound.service.ReceiveService;
import com.aaa.vo.R;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * <p>
 * 服务实现类
 * </p>
 *
 * @author aaa
 * @since 2020-11-25
 */
@Service
public class ReceiveServiceImpl extends ServiceImpl<ReceiveMapper, Receive> implements ReceiveService {
    @Autowired
    private ReceiveMapper receiveMapper;



    @Override
    public R saveReceive(String no) {
        String sub = no.substring(0, 2);
        int p = 0;
        String receiveno = "";
        Long receiveId = 0L;
        if (sub.equals("IR")) { //接货单号
            sub = no.substring(2, 4);
            receiveno = no;
            no = no.substring(2);
            p = 1;
        }
        if (sub.equals("IP")) { //采购单号
            Purchase purchase = receiveMapper.findPurchaseByNo(no);
            if(purchase == null){
                return R.error().code(500).message("单号不存在");
            }
            if (!purchase.getState().equals("2")) {
                return R.error().code(500).message("采购单未审核，不能接货");
            }
            Receive receiveP = receiveMapper.findReceiveByNo(no);
            Receive receive = new Receive();
            if (receiveP == null) {  //判断：是第一次接货
                receive.setReceiveno("IR" + no);
                receive.setPurchaseid(purchase.getId());
                receive.setPurchaseno(purchase.getPurchaseno());
                receive.setWarehouseid(purchase.getWarehouseid());
                receive.setState("0");
                receiveMapper.insert(receive);
                receiveno = receive.getReceiveno();
                QueryWrapper<Receive> queryWrapper = new QueryWrapper();
                queryWrapper.eq("receiveno",receiveno);
                Receive receive1 = receiveMapper.selectOne(queryWrapper);
                receiveId = receive1.getId();
            } else {
                String state = receiveP.getState();
                if (state.equals("2")) {
                    return R.error().code(500).message("订单已经审核，不能接货");
                }
            }
            List<PurchaseDetail> purchaseDetail = receiveMapper.findPurchaseDetailByPid(purchase.getId());
            List<Map> mapList = new ArrayList();
            if (receiveP != null) {//非首次接单
                List<ReceiveDetail> receiveDetail = receiveMapper.findReceiveDetailByNo(receiveP.getId());
                for (PurchaseDetail pd : purchaseDetail) {
                    Map map = new HashMap();
                    map.put("pid", receiveP.getId());
                    map.put("commodityid", pd.getCommodityid());
                    map.put("commoditybar", pd.getCommoditybar());
                    map.put("commodityname", pd.getCommodityname());
                    map.put("qty", pd.getQty());
                    boolean pan = true;
                    for (ReceiveDetail rd : receiveDetail) {
                        if (rd.getQty() == null) {
                            rd.setQty(0);
                        }
                        if (pd.getCommodityid().equals(rd.getCommodityid())) {
                            int max = pd.getQty() - rd.getQty();
                            pan = false;
                            map.put("max", max);
                            map.put("receiveDetailId", rd.getId());
                        }
                    }
                    if (pan) {
                        map.put("max", pd.getQty());
                        map.put("receiveDetailId", "");
                    }
                    mapList.add(map);
                }
                if (p==0){
                    receiveno = "IR"+no;
                }
                return R.ok().code(200).message("多次接货").data("mapList", mapList).data("receiveno",receiveno);
            } else {
                for (PurchaseDetail pd : purchaseDetail) {
                    Map map = new HashMap();
                    map.put("pid", receiveId);
                    map.put("commodityid", pd.getCommodityid());
                    map.put("commoditybar", pd.getCommoditybar());
                    map.put("commodityname", pd.getCommodityname());
                    map.put("qty", pd.getQty());
                    map.put("max", pd.getQty());
                    mapList.add(map);
                }
                return R.ok().code(200).message("第一次接货接货").data("mapList", mapList).data("receiveno",receiveno);
            }
        }
        if(sub.equals("AL")) { //调拨单号
            Allocation allocation = receiveMapper.findAllocationByNo(no);
            if(allocation == null){
                return R.error().code(500).message("单号不存在");
            }
            if (!allocation.getState().equals("2")) {
                return R.error().code(500).message("调拨未审核，不能接货");
            }
            Receive receiveP = receiveMapper.findReceiveALByNo(no);
            Receive receive = new Receive();
            if (receiveP == null) {  //判断：是第一次接货
                receive.setReceiveno("IR" + no);
                receive.setAllocationid(allocation.getId());
                receive.setAllocationno(no);
                receive.setWarehouseid(41L);
                receive.setState("0");
                receiveMapper.insert(receive);
                receiveno = receive.getReceiveno();
                QueryWrapper<Receive> queryWrapper = new QueryWrapper();
                queryWrapper.eq("receiveno",receiveno);
                Receive receive1 = receiveMapper.selectOne(queryWrapper);
                receiveId = receive1.getId();
            } else {
                String state = receiveP.getState();
                if (state.equals("2")) {
                    return R.error().code(500).message("订单已经审核，不能接货");
                }
            }
            List<AllocationDetail> allocationDetails = receiveMapper.findAllocationDetailByPid(allocation.getId());
            List<Map> mapList = new ArrayList();
            if (receiveP != null) { //非首次接单
                List<ReceiveDetail> receiveDetail = receiveMapper.findReceiveDetailByNo(receiveP.getId());
                for (AllocationDetail ad : allocationDetails) {
                    Map map = new HashMap();
                    map.put("pid", receiveP.getId());
                    map.put("commodityid", ad.getCommodityid());
                    map.put("commoditybar", ad.getCommoditybar());
                    map.put("commodityname", ad.getCommodityname());
                    map.put("qty", ad.getQty());
                    boolean pan = true;
                    for (ReceiveDetail rd : receiveDetail) {
                        if (rd.getQty() == null) {
                            rd.setQty(0);
                        }
                        if (ad.getCommodityid().equals(rd.getCommodityid())) {
                            int max = ad.getQty() - rd.getQty();
                            pan = false;
                            map.put("max", max);
                            map.put("receiveDetailId", rd.getId());
                        }
                    }
                    if (pan) {
                        map.put("max", ad.getQty());
                        map.put("receiveDetailId", "");
                    }
                    mapList.add(map);
                }
                if (p==0){
                    receiveno = "IR"+no;
                }
                return R.ok().code(200).message("多次接货").data("mapList", mapList).data("receiveno",receiveno);
            } else {
                for (AllocationDetail ad : allocationDetails) {
                    Map map = new HashMap();
                    map.put("pid", receiveId);
                    map.put("commodityid", ad.getCommodityid());
                    map.put("commoditybar", ad.getCommoditybar());
                    map.put("commodityname", ad.getCommodityname());
                    map.put("qty", ad.getQty());
                    map.put("max", ad.getQty());
                    mapList.add(map);
                }
                return R.ok().code(200).message("第一次接货接货").data("mapList", mapList).data("receiveno",receiveno);
            }

        }
        return R.error().message("订单号格式有误");
    }

    @Override
    public int updState(Long id, String stateno) {
        Receive receive = new Receive();
        receive.setId(id);
        receive.setState(stateno);
        int i = receiveMapper.updateById(receive);
        return i;
    }

    @Override
    public Page<Receive> searchReceive(Integer page, Integer limit, Receive receive) {
        Page<Receive> pageinfo = new Page<>(page, limit);
        String receiveno = receive.getReceiveno();
        String purchaseno = receive.getPurchaseno();
        String allocationno = receive.getAllocationno();
        String state = receive.getState();
        Long warehouseid = receive.getWarehouseid();
        QueryWrapper<Receive> queryWrapper = new QueryWrapper<>();
        if (StringUtils.isNotEmpty(purchaseno)) {
            queryWrapper.like("purchaseno", purchaseno);
        }
        if (StringUtils.isNotEmpty(receiveno)) {
            queryWrapper.like("receiveno", receiveno);
        }
        if (StringUtils.isNotEmpty(allocationno)) {
            queryWrapper.like("allocationno", allocationno);
        }
        if (StringUtils.isNotEmpty(state)) {
            queryWrapper.like("state", state);
        }
        if (warehouseid != null) {
            queryWrapper.like("warehouseid", warehouseid);
        }
        this.baseMapper.selectPage(pageinfo, queryWrapper);

        return pageinfo;
    }

    @Override
    public Receive findById(Long id) {
        Receive receive = receiveMapper.selectById(id);
        return receive;
    }
}
